//package com.hawk.eye.server.filter;
//
//import com.hawk.eye.client.toolkit.HawkEyeLogger;
//import com.hawk.eye.common.constant.AuthConstant;
//import com.hawk.eye.common.constant.HttpStatus;
//import com.hawk.eye.common.core.ApiResult;
//import com.hawk.eye.common.utils.JsonUtil;
//import com.hawk.eye.framework.cache.ICache;
//import com.hawk.eye.server.auth.ServiceAuthHolder;
//import com.hawk.eye.server.auth.ServiceAuthMetadata;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.http.MediaType;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//
///**
// * token过滤器 验证token有效性
// *
// * @author salad
// */
//@Slf4j
//@RequiredArgsConstructor
//public class AuthFilter extends OncePerRequestFilter {
//
//    private final ICache cache;
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) {
//        try {
//            String token = request.getHeader(AuthConstant.HAWK_EYE_AUTH);
//            if (token == null) {
//                renderJson(response, ApiResult.fail(HttpStatus.UNAUTHORIZED, "权限不足，令牌不合法"));
//            }
//            ServiceAuthMetadata sam = cache.<ServiceAuthMetadata>get(token);
//            if (sam == null) {
//                renderJson(response, ApiResult.fail(HttpStatus.UNAUTHORIZED, "权限不足，令牌不合法"));
//            }
//            ServiceAuthHolder.setServiceAuthMetadata(sam);
//            chain.doFilter(request, response);
//        } catch (Exception e) {
//            renderJson(response, ApiResult.fail(HttpStatus.UNAUTHORIZED, "权限不足，令牌解析失败"));
//        } finally {
//            ServiceAuthHolder.clearServiceAuthMetadata();
//        }
//
//    }
//
//
//    /**
//     * 返回json
//     */
//    private static void renderJson(HttpServletResponse response, Object result) {
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
//        try (PrintWriter out = response.getWriter()) {
//            out.append(JsonUtil.toJson(result));
//        } catch (IOException e) {
//            HawkEyeLogger.errorWithSkipSend(AuthFilter.class, "解析响应体失败：{}", e.getMessage(), e);
//        }
//    }
//}
